package com.digidata.ui

import java.awt.*

import groovy.swing.SwingBuilder
import java.awt.BorderLayout
import javax.swing.*
import junit.framework.TestFailure
import junit.framework.TestResult
import junit.framework.TestSuite

import com.digidata.rest.testing.engine.TestRunner


class GroovyInterface {
	
	static void main(args) {
		java.util.List testsToPass = ["test_1", "test_2", "test_3", "test_4"]
		UserInterface ui = new UserInterface(testsToPass)
		ui.createTestSelectionWindow()
	}
}

class UserInterface {
	
	java.util.List testList = ["Failed to load tests!"]
	java.util.List selectedTests = []
	java.util.List unselectedTests = []
	Frame testSelectionFrame
	Frame testResultFrame
	TestRunner testRunner
	
	UserInterface(){}

	//UserInterface(java.util.List allTests){
	//	testList = allTests
	//}
	
	UserInterface(String pathToTestDirectory){
		//test runner loads all tests in the path but waits to execute until tests are selected
		this.testRunner = new TestRunner(pathToTestDirectory)
		testList = []
		testRunner.suiteList.each{
			it.tests().each{
				testList = testList + it.name
			}
		}
	}

	public void createTestSelectionWindow(){
		SwingBuilder swing = new SwingBuilder()
		def mainTestList = new JList()
		def BL = new BorderLayout()
		mainTestList.selectionMode = ListSelectionModel.MULTIPLE_INTERVAL_SELECTION
		mainTestList.setListData(testList)
		
		
		def leftList = swing.list(testList, name:"leftList", selectionMode:ListSelectionModel.MULTIPLE_INTERVAL_SELECTION) 
		def rightList = swing.list(name:"rightList")
		def selectButton = swing.button(text:"Select Test(s)", actionPerformed:{
					def leftTemp = leftList.getSelectedValues()
					def rightTemp = rightList.getSelectedValues()
					//println leftTemp
					//println rightTemp
					selectedTests.addAll(leftTemp)
					selectedTests.removeAll(rightTemp)
					unselectedTests = []
					unselectedTests.addAll(testList)
					unselectedTests.removeAll(selectedTests)
		
					rightList.setListData(selectedTests.asType(String[]))
					leftList.setListData(unselectedTests.asType(String[]))
				})
		def startTestButton = swing.button(text:"Start Test(s)", actionPerformed:{
			createTestResultWindow()
			})
		
		testSelectionFrame = swing.frame(title:'Testing Engine UI', size:[800,358], layout:new BorderLayout(), defaultCloseOperation:JFrame.EXIT_ON_CLOSE, show:true){
			lookAndFeel("system")
			menuBar{
				
				menu('File'){
					menuItem(text:"Exit", actionPerformed: {dispose()})
				}
			}
			splitPane(orientation:JSplitPane.HORIZONTAL_SPLIT, dividerLocation:380){
				scrollPane(name:"left", constraints:"left"){
					widget(leftList)
				}
				scrollPane(name:"right", constraints:"right"){
					widget(rightList)
				}
			}
			panel(constraints:BL.SOUTH){
				widget(selectButton)
				widget(startTestButton)
				
			}
		}
	}
	
	public void createTestResultWindow(def testsToRun){
		SwingBuilder swing = new SwingBuilder()
		def BL = new BorderLayout()
		//call the addTests()
		//Send it the list of selected tests
		testRunner.addTests(selectedTests)
		
		def TestResult result = new TestResult()
		testRunner.runTest(result)
		
		Map<String, String> testResultMap = [:]
		Map<String, TestFailure> errorMap = [:]
		Map<String, TestFailure> failureMap = [:]
		
		result.errors().each{
			errorMap[it.failedTest().name] = it
		}
		result.failures().each{
			failureMap[it.failedTest().name] = it
		}
		
		testRunner.customSuite.tests().each{
			if(errorMap[it.name] != null)
				testResultMap[it.name] = errorMap[it.name].toString()
			else if(failureMap[it.name] != null)
				testResultMap[it.name] = failureMap[it.name].toString()
			else
				testResultMap[it.name] = "Pass"
		}
		
		println testResultMap
		
		testResultFrame = swing.frame(title:'Test Progress Window', size:[500,358], layout:new BorderLayout(), defaultCloseOperation:JFrame.DISPOSE_ON_CLOSE, show:true){
			lookAndFeel("system")
			menuBar{
				menu('File'){
					menuItem(text:"Close", actionPerformed: {dispose()})
				}
			}
			scrollPane(constraints:BL.CENTER){
				String tests = ""
				testsToRun = selectedTests
				testsToRun.each{
					if(it != null)
						tests += it + " - testing in progress..." + "\n"
				}
				textArea(tests, editable:false)
			}
		}
	}
}


